package com.example.city_home_app.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.example.city_home_app.Bean.Down_Login;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InitActivity extends BaseActivity {
    @BindView(R.id.btn_init)
    Button btnInit;
    //记住密码状态 TRUE为记住密码
    private boolean RemeberPassword;
    private String LoginName;
    private String LoginPassword;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private int can_login = 3;
    private boolean islogin = false;
    private Thread thread;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_init;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(UIUtils.PERMISSIONS, 0);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (can_login!=0) {
                    try {
                        Thread.sleep(1000);
                        can_login--;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                btnInit.setText("跳过("+can_login+")");
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(!islogin) {
                    login();
                }
            }
        });
        thread.start();
    }

    private RetrofitListener<Down_Login> loginRetrofitListener = new RetrofitListener<Down_Login>() {
        @Override
        public void onResponse(Down_Login listener) {
            if (listener.getMessage().equals("success")) {
                BaseActivity.setUserinfo(listener);
                Intent intent = new Intent(InitActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                Intent intent = new Intent(InitActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };


    private void login() {
        sharedPreferences = getSharedPreferences("Logininfo", Context.MODE_PRIVATE);
        LoginName = sharedPreferences.getString("LoginName", "");
        LoginPassword = sharedPreferences.getString("LoginPassword", "");
        ApiModel model = new ApiModel();
        model.login(LoginName, LoginPassword, loginRetrofitListener);
    }

    @OnClick(R.id.btn_init)
    public void onViewClicked() {
        islogin = true;
        login();
    }
}
